package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.sound.midi.Track;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;
//http://127.0.0.1/api/album/trackInfo/uploadTrack
//	上传音频
	@Operation(summary = "上传音频")
	@PostMapping("/uploadTrack")
	public Result uploadTrack(MultipartFile file){
		Map<String,Object> map =  trackInfoService.uploadTrack(file);
		return Result.ok(map);
	}
//	http://127.0.0.1/api/album/trackInfo/saveTrackInfo
	@TsLogin
	@Operation(summary = "声音保存")
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
//		从线程中获取userId
		Long userId = AuthContextHolder.getUserId()==null?1L:AuthContextHolder.getUserId();
		trackInfoService.saveTrackInfo(userId,trackInfoVo);
		return Result.ok();
	}
//	http://127.0.0.1/api/album/trackInfo/findUserTrackPage/1/10
	@TsLogin
	@Operation(summary = "查询声音列表")
	@PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
	public Result findUserTrackPage(@PathVariable Long pageNo,
									@PathVariable Long pageSize,
									@RequestBody TrackInfoQuery trackInfoQuery){
		Page<TrackListVo> page = new Page<>(pageNo,pageSize);
//		从线程中获取userId
		trackInfoQuery.setUserId(AuthContextHolder.getUserId()==null?1L:AuthContextHolder.getUserId());
		IPage<TrackListVo> list = trackInfoService.findUserTrackPage(page,trackInfoQuery);
		return Result.ok(list);
	}
//	http://127.0.0.1/api/album/trackInfo/removeTrackInfo/51977
	@Operation(summary = "删除声音")
	@DeleteMapping("/removeTrackInfo/{trackId}")
	public Result removeTrackInfo(@PathVariable Long trackId){
		trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}
//	http://127.0.0.1/api/album/trackInfo/getTrackInfo/51982
	@Operation(summary = "修改时回显声音")
	@GetMapping("/getTrackInfo/{trackId}")
	public Result<TrackInfo> getTrackInfo(@PathVariable Long trackId){
		TrackInfo trackInfo = trackInfoService.getTrackInfo(trackId);
		return Result.ok(trackInfo);
	}
//	http://127.0.0.1/api/album/trackInfo/updateTrackInfo/51982
//	修改后保存数据
	@Operation(summary = "修改后保存数据")
	@PutMapping("/updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable Long trackId,@RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(trackId,trackInfoVo);
		return Result.ok();
	}
//	http://127.0.0.1/api/album/trackInfo/findAlbumTrackPage/426/1/10
	@TsLogin
	@Operation(summary = "专辑信息回显的时候分页查询专辑下的声音")
	@GetMapping("/findAlbumTrackPage/{albumId}/{pageNo}/{pageSize}")
	public Result findAlbumTrackPage(@PathVariable Long albumId,
									 @PathVariable Long pageNo,
									 @PathVariable Long pageSize){
		Page<AlbumTrackListVo> page = new Page<>(pageNo,pageSize);
		IPage<AlbumTrackListVo> pageList = trackInfoService.findAlbumTrackPage(page,albumId);
		return Result.ok(pageList);
	}
//	http://127.0.0.1/api/album/trackInfo/findUserTrackPaidList/20759
	@TsLogin
	@Operation(summary = "获取用户未购买的所有声音")
	@GetMapping("/findUserTrackPaidList/{trackId}")
	public Result findUserTrackPaidList(@PathVariable Long trackId){
//		从线程中获取userId
		Long userId = AuthContextHolder.getUserId();
//		调用service层的方法
 		List<Map<String,Object>> list = trackInfoService.findUserTrackPaidList(trackId,userId);
//		返回数据
		 return Result.ok(list);
	}

	@TsLogin
	@Operation(summary = "找到用户购买声音列表")
	@GetMapping("/findPaidTrackInfoList/{trackId}/{trackCount}")
	public Result<List<TrackInfo>> getTrackInfoList(@PathVariable Long trackId, @PathVariable Integer trackCount){
//		从线程中获取userId
		Long userId = AuthContextHolder.getUserId();
//		调用service层的方法
		List<TrackInfo> list = trackInfoService.getTrackInfoList(trackId,trackCount,userId);
		return Result.ok(list);
	}

}

